<?php
namespace app\api\controller\works;
use app\common\controller\Api;
use think\Db;
use app\common\model\Attachment;
class Photo extends Api{
// 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    protected function _initialize()
    {
        parent::_initialize();

    }

    public function del(){
        $userid = $this->auth->id;
        $ids = $this->request->post('ids');
        if(empty($ids)){
            $this->error('参数错误');
        }
        $data = explode(',',$ids);
        if(is_array($data)){
            foreach ($data as $key => $value) {
                Db::name('WorksPhoto')->where('id',$value)->delete();
            }
        }
        $this->success('作品图片删除成功');
    }
    public function sort(){
        $userid = $this->auth->id;
        $data = $this->request->post('data');
        if($data){
            foreach ($data as $key => $value) {
                Db::name('WorksPhoto')->where('id',$value['id'])->update(['weigh'=>$value['weigh']]);
            }
        }
        $this->success('相册排序完成');
    }

    public function move(){
        $userid = $this->auth->id;
        $images = $this->request->post('images/a');
        $album_id = $this->request->post('id');
        if(empty($album_id)){
            $this->error('作品ID为空');
        }
        if(empty($images)){
            $this->error('没选择相册图片');
        }
        $photo = Db::name('AlbumPhoto')->where('id','in',array_column($images,'id'))->column('id,image');
        $count = Db::name('WorksPhoto')->where('works_catalog_id',$album_id)->count('id')?:0;
        if(!empty($images)){
            foreach ($images as $key => $value) {
                $images[$key]['image'] = $photo[$value['id']];
                $images[$key]['weigh'] = $count+$value['weigh']; 
                $images[$key]['works_catalog_id'] = $album_id;
                $images[$key]['createtime'] = time();
                $images[$key]['updatetime'] = time(); 
            }
            Db::name('WorksPhoto')->insertAll($images);
        }
        $this->success('相册图片移动完成');
    }
}